package org.example.xin_yue_song.ui.screens

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonColors
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import org.example.xin_yue_song.R
import org.example.xin_yue_song.ui.components.XYInputComponent
import org.example.xin_yue_song.ui.components.XYPasswordInputComponent

@Composable
fun LoginScreen(modifier: Modifier = Modifier) {
  var phone by remember { mutableStateOf("") }
  var password by remember { mutableStateOf("") }
  var isAgree by remember { mutableStateOf(false) }

  Surface(
    modifier = modifier.fillMaxSize()
  ) {
    Column(
      horizontalAlignment = Alignment.CenterHorizontally,
      modifier = Modifier
        .padding(vertical = 60.dp, horizontal = 16.dp)
        .fillMaxHeight()
    ) {
      Text(
        text = stringResource(R.string.welcome_login_title),
        fontSize = 36.sp
      )
      Spacer(modifier = Modifier.height(8.dp))
      Image(
        painter = painterResource(R.drawable.ktv),
        contentDescription = stringResource(R.string.logo_description),
        Modifier.size(56.dp)
      )
      Spacer(modifier = Modifier.height(12.dp))

      XYInputComponent(R.string.input_phone,phone) { newPhone -> phone = newPhone }
      XYPasswordInputComponent(R.string.input_password,password) { newPassword -> password = newPassword }

      Row(
        horizontalArrangement = Arrangement.SpaceBetween,
        modifier = Modifier.width(200.dp).padding(vertical = 8.dp)
      ) {
        Text(
          text = stringResource(R.string.forget_password),
          fontSize = 12.sp,
          color = Color(0xFF4AB0F8),
        )
        Text(
          text = stringResource(R.string.haven_register),
          fontSize = 12.sp,
          color = Color(0xFF4AB0F8),
        )
      }

      Button(
        onClick = {},
        shape = RoundedCornerShape(16.dp),
        colors = ButtonColors(
          containerColor = Color(0xFF4AB0F8),
          contentColor = Color(0xFFFFFFFF),
          disabledContentColor = Color(0xFF4AB0F8),
          disabledContainerColor = Color(0xFF4AB0F8)
        ),
        modifier = Modifier.width(140.dp)
      ) {
        Text(text = stringResource(R.string.login), fontSize = 16.sp, fontWeight = FontWeight.Bold)
      }

      Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.Center,
        modifier = Modifier.width(320.dp).padding(vertical = 8.dp)
      ) {
        RadioButton(
          selected = isAgree,
          onClick = { isAgree = !isAgree },
          colors = RadioButtonColors(
            selectedColor = Color(0XFF575757),
            unselectedColor = Color(0XFFB5B5B5),
            disabledSelectedColor = Color(0XFF575757),
            disabledUnselectedColor = Color(0XFF575757),
          ),
          modifier = Modifier
        )
        Text(
          text = buildAnnotatedString {
            withStyle(style = SpanStyle(color = Color(0xFFB5B5B5), fontSize = 12.sp,)) {
              append("阅读同意")
            }
            withStyle(style = SpanStyle(color = Color(0xFF575757), fontSize = 12.sp,)) {
              append("《许可协议》")
            }
            withStyle(style = SpanStyle(color = Color(0xFF575757), fontSize = 12.sp,)) {
              append("《隐私指引》")
            }
          }
        )
      }
    }
  }
}

@Preview(backgroundColor = 0xffffff)
@Composable
private fun LoginScreenPreview() {
  LoginScreen()
}